package com.bsoft.exchange.controller.examination;

import com.bsoft.exchange.param.DetailsParams;
import com.bsoft.exchange.param.ItemsParams;
import com.bsoft.exchange.param.MedicalAppointmentParams;
import com.bsoft.exchange.response.ResponseEntity;
import com.bsoft.exchange.service.MedicalAppointmentDetailService;
import com.bsoft.exchange.service.MedicalAppointmentService;
import com.bsoft.exchange.service.ReportItemDetailService;
import com.bsoft.exchange.vo.MedicalAppointmentDetailVO;
import com.bsoft.exchange.vo.MedicalAppointmentVO;
import com.bsoft.exchange.vo.ReportItemDetailVO;
import com.bsoft.exchange.vo.ReportItemsDetailsVO;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("examination")
public class ExaminationController {

    @Autowired
    private MedicalAppointmentDetailService medicalAppointmentDetailService;

    @Autowired
    private MedicalAppointmentService medicalAppointmentService;

    @Autowired
    private ReportItemDetailService reportItemDetailService;

    private Mapper mapper = DozerBeanMapperBuilder.buildDefault();

    @ResponseBody
    @RequestMapping(value = "/medicalAppointmentList", method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<List<MedicalAppointmentVO>> medicalAppointmentList(@RequestBody MedicalAppointmentParams medicalAppointmentParams) {
        ResponseEntity<List<MedicalAppointmentVO>> responseEntity = new ResponseEntity<>();
        try {
            List<MedicalAppointmentVO> list = medicalAppointmentService.getListByParams(medicalAppointmentParams.getIdentityType(), medicalAppointmentParams.getIdentityNo(), medicalAppointmentParams.getOrgId());
            responseEntity.setData(list);
            responseEntity.setMsg("查询成功！");
            responseEntity.setCode(200L);
        } catch (Exception e) {
            e.printStackTrace();
            responseEntity.setCode(500L);
            responseEntity.setMsg("服务器异常！");
        }
        return responseEntity;
    }

    @ResponseBody
    @RequestMapping(value = "/medicalAppointmentDetail", method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<MedicalAppointmentDetailVO> medicalAppointmentDetail(@RequestBody DetailsParams detailsParams) {
        ResponseEntity<MedicalAppointmentDetailVO> responseEntity = new ResponseEntity<>();
        try {
            MedicalAppointmentDetailVO appointmentDetailVO = medicalAppointmentDetailService.getDetailByParams(detailsParams.getRegRecordCode(), detailsParams.getIdentityType(), detailsParams.getIdentityNo(), detailsParams.getOrgId());
            responseEntity.setCode(200L);
            responseEntity.setMsg("查询成功！");
            responseEntity.setData(appointmentDetailVO);
        } catch (Exception e) {
            e.printStackTrace();
            responseEntity.setCode(500L);
            responseEntity.setMsg("服务器异常！");
        }
        return responseEntity;
    }

    @ResponseBody
    @RequestMapping(value = "/reportItemsDetails", method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<ReportItemsDetailsVO> reportItemsDetails(@RequestBody ItemsParams itemsParams) {
        ResponseEntity<ReportItemsDetailsVO> responseEntity = new ResponseEntity<>();
        try {
            List<ReportItemDetailVO> reportItems = reportItemDetailService.getReportItems(itemsParams.getRegRecordCode(), itemsParams.getIdentityType(), itemsParams.getIdentityNo(), itemsParams.getOrgCode());
            ReportItemsDetailsVO reportItemsDetailsVO = new ReportItemsDetailsVO();
            if(reportItems != null && reportItems.size() > 0){
                reportItemsDetailsVO.setDeptCode(reportItems.get(0).getDeptCode());
                reportItemsDetailsVO.setDeptName(reportItems.get(0).getDeptName());
                reportItemsDetailsVO.setExamCode(reportItems.get(0).getExamCode());
                reportItemsDetailsVO.setExamName(reportItems.get(0).getExamName());
                reportItemsDetailsVO.setResultDoc(reportItems.get(0).getResultDoc());
                reportItemsDetailsVO.setResultDt(reportItems.get(0).getResultDt());
                reportItemsDetailsVO.setResultSummary(reportItems.get(0).getResultSummary());
            }
            List<ReportItemsDetailsVO.Item> items = reportItems.stream().map(reportItemDetailVO -> {
                ReportItemsDetailsVO.Item item = mapper.map(reportItemDetailVO, ReportItemsDetailsVO.Item.class);
                return item;
            }).collect(Collectors.toList());
            reportItemsDetailsVO.setItems(items);
            responseEntity.setData(reportItemsDetailsVO);
            responseEntity.setMsg("查询成功！");
            responseEntity.setCode(200L);
        } catch (Exception e) {
            e.printStackTrace();
            responseEntity.setMsg("服务器异常！");
            responseEntity.setCode(500L);
        }
        return responseEntity;
    }
}
